package com.leyou.sh.item.web;

import com.leyou.sh.common.dto.PageDTO;
import com.leyou.sh.item.dto.BrandDTO;
import com.leyou.sh.item.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author balisong_fcs
 * @date 2020/12/27 19:48
 * @desc
 */
@RestController
@RequestMapping("/brand")
public class BrandController {
    @Autowired
    private BrandService brandService;

    /**
     * 根据id查询品牌
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseEntity<BrandDTO> queryBrandById(@PathVariable("id") Long id) {
        return ResponseEntity.ok(this.brandService.queryBrandById(id));
    }

    /**
     * 根据id集合查询品牌集合
     * @param ids
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<BrandDTO>> queryBrandByIds (@RequestParam("ids")List<Long> ids){
        return ResponseEntity.ok(this.brandService.queryBrandListByIds(ids));
    }

    /**
     * 分页查询品牌
     * @param page 当前页
     * @param rows 每页显示条数
     * @param key 查询条件 判断字段对应的模糊查询还是精准查询
     * @return
     */
    @GetMapping("/page")
    public ResponseEntity<PageDTO> queryBrandByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
            @RequestParam(value = "key", required = false) String key
    ) {

        return ResponseEntity.ok(this.brandService.queryByPage(page,rows,key));
    }

    /**
     * 根据分类id查询品牌
     * @param id
     * @return
     */
    @GetMapping("/of/category")
    public ResponseEntity<List<BrandDTO>> queryBrandByCategroyId (
            @RequestParam("id") Integer id){
        return ResponseEntity.ok(this.brandService.queryBrandByCategroyId(id));
    }

    /**
     * 新增品牌
     * @param brandDTO
     * @return
     */
    @PostMapping
    public ResponseEntity<Void> saveBrand(BrandDTO brandDTO) {
        brandService.saveBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 跟新品牌
     * @param brandDTO
     * @return
     */
    @PutMapping
    public ResponseEntity<Void> updateBrand(BrandDTO brandDTO){
        this.brandService.updateBrand(brandDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deleteBrandById (@PathVariable("id") Integer id){
        this.brandService.deleteByBrandId(id);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
